package com.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;

import com.entity.StoreupEntity;
import com.entity.view.StoreupView;

import com.service.StoreupService;
import com.utils.PageUtils;
import com.utils.MPUtil;

/**
 * 收藏表
 * 后端接口
 * @author team10
 * @email
 *
 */
@RestController
@RequestMapping("/storeup")
public class StoreupController {
	@Autowired
	private StoreupService storeupService;

	// 后端列表接口
	/**
	 * 后端列表
	 * 根据条件分页查询收藏数据
	 */
	@RequestMapping("/page")
	public R page(@RequestParam Map<String, Object> params, StoreupEntity storeup, HttpServletRequest request){
		// 非管理员用户只显示当前用户的收藏
		if(!request.getSession().getAttribute("role").toString().equals("管理员")) {
			storeup.setUserid((Long)request.getSession().getAttribute("userId"));
		}
		EntityWrapper<StoreupEntity> ew = new EntityWrapper<StoreupEntity>();
		// 使用自定义工具类MPUtil进行分页查询
		PageUtils page = storeupService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, storeup), params), params));
		return R.ok().put("data", page);
	}

	// 前端列表接口
	/**
	 * 前端列表
	 * 根据条件分页查询收藏数据
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params, StoreupEntity storeup, HttpServletRequest request){
		// 非管理员用户只显示当前用户的收藏
		if(!request.getSession().getAttribute("role").toString().equals("管理员")) {
			storeup.setUserid((Long)request.getSession().getAttribute("userId"));
		}
		EntityWrapper<StoreupEntity> ew = new EntityWrapper<StoreupEntity>();
		// 使用自定义工具类MPUtil进行分页查询
		PageUtils page = storeupService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, storeup), params), params));
		return R.ok().put("data", page);
	}

	// 列表接口
	/**
	 * 列表
	 * 查询所有符合条件的收藏数据
	 */
	@RequestMapping("/lists")
	public R list(StoreupEntity storeup){
		EntityWrapper<StoreupEntity> ew = new EntityWrapper<StoreupEntity>();
		// 使用自定义工具类MPUtil进行等值查询
		ew.allEq(MPUtil.allEQMapPre(storeup, "storeup"));
		return R.ok().put("data", storeupService.selectListView(ew));
	}

	// 查询接口
	/**
	 * 查询
	 * 根据条件查询一条收藏记录
	 */
	@RequestMapping("/query")
	public R query(StoreupEntity storeup){
		EntityWrapper<StoreupEntity> ew = new EntityWrapper<StoreupEntity>();
		// 使用自定义工具类MPUtil进行等值查询
		ew.allEq(MPUtil.allEQMapPre(storeup, "storeup"));
		StoreupView storeupView = storeupService.selectView(ew);
		return R.ok("查询收藏表成功").put("data", storeupView);
	}

	// 后端详情接口
	/**
	 * 后端详情
	 * 根据ID查询收藏记录
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Long id){
		StoreupEntity storeup = storeupService.selectById(id);
		return R.ok().put("data", storeup);
	}

	// 前端详情接口
	/**
	 * 前端详情
	 * 根据ID查询收藏记录
	 */
	@IgnoreAuth
	@RequestMapping("/detail/{id}")
	public R detail(@PathVariable("id") Long id){
		StoreupEntity storeup = storeupService.selectById(id);
		return R.ok().put("data", storeup);
	}

	// 后端保存接口
	/**
	 * 后端保存
	 * 保存新的收藏记录
	 */
	@RequestMapping("/save")
	public R save(@RequestBody StoreupEntity storeup, HttpServletRequest request){
		// 使用当前时间和随机数生成唯一的ID
		storeup.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
		// 设置当前用户ID
		storeup.setUserid((Long)request.getSession().getAttribute("userId"));
		storeupService.insert(storeup);
		return R.ok();
	}

	// 前端保存接口
	/**
	 * 前端保存
	 * 保存新的收藏记录
	 */
	@RequestMapping("/add")
	public R add(@RequestBody StoreupEntity storeup, HttpServletRequest request){
		// 使用当前时间和随机数生成唯一的ID
		storeup.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
		// 设置当前用户ID
		storeup.setUserid((Long)request.getSession().getAttribute("userId"));
		storeupService.insert(storeup);
		return R.ok();
	}

	// 修改接口
	/**
	 * 修改
	 * 更新收藏记录
	 */
	@RequestMapping("/update")
	public R update(@RequestBody StoreupEntity storeup, HttpServletRequest request){
		storeupService.updateById(storeup); // 全部更新
		return R.ok();
	}


	// 删除接口
	/**
	 * 删除
	 * 根据ID删除收藏记录
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] ids){
		storeupService.deleteBatchIds(Arrays.asList(ids));
		return R.ok();
	}

	// 提醒接口
	/**
	 * 提醒接口
	 * 用于提醒收藏数据相关操作
	 */
	@RequestMapping("/remind/{columnName}/{type}")
	public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request,
						 @PathVariable("type") String type, @RequestParam Map<String, Object> map) {
		map.put("column", columnName);
		map.put("type", type);

		if(type.equals("2")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			Date remindStartDate = null;
			Date remindEndDate = null;
			if(map.get("remindstart") != null) {
				Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
				c.setTime(new Date());
				c.add(Calendar.DAY_OF_MONTH, remindStart);
				remindStartDate = c.getTime();
				map.put("remindstart", sdf.format(remindStartDate));
			}
			if(map.get("remindend") != null) {
				Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
				c.setTime(new Date());
				c.add(Calendar.DAY_OF_MONTH, remindEnd);
				remindEndDate = c.getTime();
				map.put("remindend", sdf.format(remindEndDate));
			}
		}

		Wrapper<StoreupEntity> wrapper = new EntityWrapper<StoreupEntity>();
		if(map.get("remindstart") != null) {
			wrapper.ge(columnName, map.get("remindstart"));
		}
		if(map.get("remindend") != null) {
			wrapper.le(columnName, map.get("remindend"));
		}
		// 非管理员用户只能查看自己的提醒
		if(!request.getSession().getAttribute("role").toString().equals("管理员")) {
			wrapper.eq("userid", (Long)request.getSession().getAttribute("userId"));
		}
		int count = storeupService.selectCount(wrapper);
		return R.ok().put("count", count);
	}
}
